/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db.connectionPool;

import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Omega Zero
 */
public class DbConnectionManager {
    private DbConnectionPool pool;
    private static DbConnectionManager instance=null;


     public static DbConnectionManager getInstance() {
        if (instance == null) {
             instance = new DbConnectionManager();
        }
        return instance;
    }


    private DbConnectionManager()/* throws SQLException, IOException, Exception*/{
        try {
            pool = new DbConnectionPool();
            //pool.configWatcher();
        } catch (SQLException ex) {
            Logger.getLogger(DbConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DbConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(DbConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public DbConnection getConnection() throws DBPoolException{
        if(pool.isAvailable()){
            try {
                return pool.getDbConnection();
            } catch (Exception ex) {
                Logger.getLogger(DbConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }

    public void closeDbConnection(DbConnection connnection) throws Exception{
        pool.returnDbConnection(connnection);
    }


    public void changePoolAvailability(boolean poolState){
        this.pool.changeAvailability(poolState);
    }

    public void reSizePool(int size) throws Exception{
        this.pool.reSizePool(size);
    }

}
